package view;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.util.Rotation;

import utils.ScreenAdjustment;

//@author A0097788Y
public class PieChart extends JFrame{

	private String _title;
	private DefaultPieDataset _dataset = new DefaultPieDataset();

	/**
	 * @param applicationTitle
	 * @param chartTitle
	 */
	public PieChart(String applicationTitle, String chartTitle) {
		super(applicationTitle);
		_title = chartTitle;
	}

	/**
	 * 
	 * @Precondition 
	 * @Postcondition 
	 */
	public void create(){
		PieDataset dataset = _dataset;
		JFreeChart chart = createChart(dataset, _title);
		// we put the chart into a panel
		ChartPanel chartPanel = new ChartPanel(chart);
		// default size
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
		this.setBounds(ScreenAdjustment.getAlignCenterX(750),ScreenAdjustment.getAlignCenterY(270), 750, 270);
		// add it to our application
		setContentPane(chartPanel);
		this.pack();
		this.setVisible(true);
	}

	//add
	/**
	 * 
	 * @param label
	 * @param amount
	 * @Precondition 
	 * @Postcondition 
	 */
	public void add(String label, double amount) {
		_dataset.setValue(label, amount);
	}

	//create the pie chart
	/**
	 * 
	 * @param dataset
	 * @param title
	 * @return
	 * @Precondition 
	 * @Postcondition 
	 */
	private JFreeChart createChart(PieDataset dataset, String title) {

		JFreeChart chart = ChartFactory.createPieChart3D(title,          // chart title
				dataset,                // data
				true,                   // include legend
				true,
				false);

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		plot.setStartAngle(135);
		plot.setDirection(Rotation.CLOCKWISE);
		plot.setForegroundAlpha(0.5f);
		return chart;

	}
}
